package com.zmn.oms.third.dj58.service.impl;

import org.apache.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.constant.StatusConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.amount.AmountUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.third.dj58.DaoJia58Consts;
import com.zmn.oms.third.dj58.dto.api.DaoJia58Response;
import com.zmn.oms.third.dj58.service.VasOrderBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkQueryBService;
import com.zmn.vas.common.dio.order.OrderCancelDIO;
import com.zmn.vas.common.dio.order.OrderDIO;
import com.zmn.vas.dubbo.consts.VasDubboConsts;
import com.zmn.vas.dubbo.interfaces.order.VasOrderListRemoteService;
import com.zmn.vas.dubbo.interfaces.order.VasOrderManagerModifyRemoteService;
import com.zmn.vas.dubbo.interfaces.order.VasOrderModifyRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;

import static com.zmn.oms.third.dj58.DaoJia58Consts.*;

/**
 * @author hyj
 * @version v1.0
 * @since 2020/6/24 16:33
 **/
@Slf4j
@Service
public class VasOrderBServiceImpl implements VasOrderBService {
    public static final int INPUT_MODE = 2;
    @Autowired
    protected ZsOrderWorkQueryBService zsOrderWorkQueryBService;

    @Reference(version = VasDubboConsts.INTERFACE_VERSION, check = false)
    private VasOrderModifyRemoteService vasOrderModifyRemoteService;

    @Reference(version = VasDubboConsts.INTERFACE_VERSION, check = false)
    private VasOrderListRemoteService vasOrderListRemoteService;

    @Reference(version = VasDubboConsts.INTERFACE_VERSION, check = false)
    private VasOrderManagerModifyRemoteService vasOrderManagerModifyRemoteService;

    @Override
    public void cancelInsure(DaoJia58Response daoJia58Response, JSONObject data){


        OrderCancelDIO orderCancelDIO = new OrderCancelDIO();
        orderCancelDIO.setOrderId(Long.valueOf(data.getString("insureId")));
        orderCancelDIO.setCancelTime(data.getDate("cancelTime"));
        orderCancelDIO.setContent(data.getString("cancelReason"));
        orderCancelDIO.setOperator(CHANNEL_NAME);
        orderCancelDIO.setOperatorId((long) CHANNEL_ID_API);
        orderCancelDIO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_CHANNEL);

        log.info("【{}】取消保单服务 Request: ThirdOrderCancelDIO=[{}]", DaoJia58Consts.CHANNEL_ID_DJ58_REWORK, orderCancelDIO);
        ResponseDTO cancelResponseDTO = vasOrderManagerModifyRemoteService.cancel(orderCancelDIO);

        if (cancelResponseDTO.isSuccess()) {
            daoJia58Response.setCode(GlobalConsts.NONE);
        } else {
            log.info("【{}】取消保单服务 Response: ResponseDTO=[{}]", DaoJia58Consts.CHANNEL_ID_DJ58_REWORK, JSON.toJSONString(cancelResponseDTO));
            daoJia58Response.setCode(StatusConsts.ERROR_UNKNOWN);
            daoJia58Response.setMessage(cancelResponseDTO.getMessage());
        }
    }

    @Override
    public void createInsure(DaoJia58Response daoJia58Response, String daojiaJson)throws OmsBaseException {
        JSONObject data = JSON.parseObject(daojiaJson);
        String orderId = data.getString("orderId");

        // 换取平台工单信息
        EsOrderWork orderWork = zsOrderWorkQueryBService.findEsOrderWorkByOuterIdAndChannelIds(orderId,
                DaoJia58Consts.getAllChannelIds());
        if (Objects.isNull(orderWork)) {
            log.error("#oms#insureCreateRequest outerId:{} 未查询到订单", orderId);
            throw new OmsBaseException(DaoJia58Consts.ZMN_RETURN_CODE_SERVER_ERROR, "创建保单失败 订单不存在");
        }
        OrderDIO dio = new OrderDIO();
        BeanUtils.copyProperties(orderWork, dio);
        dio.setWorkId(orderWork.getOrderId());
        dio.setUserName(data.getString("relName"));
        dio.setMobile(data.getString("phone"));
        dio.setAddress(data.getString("address"));
        String moneyStr = String.valueOf(data.getBigDecimal("money"));
        dio.setAmount(StringUtils.isEmpty(moneyStr) ? null : AmountUtil.yuan2FenInt(moneyStr));
        dio.setThirdWorkId(Long.valueOf(orderId));
        dio.setLocation(data.getString("location"));
        dio.setCompanyId(orderWork.getCompanyId());
        dio.setMasterId(orderWork.getMasterId());
        dio.setMasterName(orderWork.getMasterName());
        dio.setInputMode(INPUT_MODE);
        dio.setManageCompanyId(orderWork.getManageCompanyId());
        dio.setSellerType(DAOJIA58_SELL_TYPE);
        dio.setSellerName(CHANNEL_NAME);
        dio.setOperatorId(Long.valueOf(CHANNEL_ID_API));
        dio.setOperator(CHANNEL_NAME);
        dio.setSellerId(CHANNEL_ID_API);
        dio.setProductId(PRODUCT_ID);
        log.info("【{}】创建保单服务 Request: MemOrderInsureDIO=[{}]", DaoJia58Consts.CHANNEL_ID_DJ58_REWORK,
                JSON.toJSONString(dio));
        com.zmn.common.dto2.ResponseDTO<Long> responseDTO = vasOrderModifyRemoteService.createOrder(dio);
        if (responseDTO.isSuccess()) {
            daoJia58Response.setData(String.valueOf(responseDTO.getData()));
            daoJia58Response.setCode(GlobalConsts.NONE);
        } else {
            log.info("【{}】创建保单服务 Response: ResponseDTO=[{}]", DaoJia58Consts.CHANNEL_ID_DJ58_REWORK,
                    JSON.toJSONString(responseDTO));
            daoJia58Response.setCode(StatusConsts.ERROR_UNKNOWN);
            daoJia58Response.setMessage(responseDTO.getMessage());
        }
    }
}

